package com.project.sianet.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.project.sianet.R;
import com.project.sianet.data.LibretaFragmentPagerAdapter;
import com.project.sianet.entidades.Alumno;
import com.project.sianet.entidades.Libreta;
import com.project.sianet.util.SessionManager;
import com.viewpagerindicator.TabPageIndicator;

public class LibretaFragment extends Fragment {
	
	private static int mSubTitleFrag;
	private static String mTitleFrag, usuarioLogeado;
	private static List<Libreta> mLibreta;
	
	public LibretaFragment() { }
	
	public static final LibretaFragment newInstance(List<Libreta> libreta) {
		LibretaFragment f = new LibretaFragment();
		Bundle args = new Bundle();
		args.putString("mTitleFrag", null);
		args.putInt("mSubTitleFrag", R.string.title_bar_libreta);
		args.putString("usuarioLogeado", SessionManager.usuarioLogeado());
		args.putParcelableArrayList("mLibreta", (ArrayList<Libreta>) libreta);
		f.setArguments(args);
	    return f;
	}
	
	public static final LibretaFragment newInstance(Alumno alu, List<Libreta> libreta) {
		LibretaFragment f = new LibretaFragment();
		Bundle args = new Bundle();
		args.putString("mTitleFrag", alu.getNombreCompleto());
		args.putInt("mSubTitleFrag", R.string.title_bar_libreta);
		args.putString("usuarioLogeado", alu.login);
		args.putParcelableArrayList("mLibreta", (ArrayList<Libreta>) libreta);
		f.setArguments(args);
	    return f;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_libreta, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		mTitleFrag = getArguments().getString("mTitleFrag");
		mSubTitleFrag = getArguments().getInt("mSubTitleFrag");
		usuarioLogeado = getArguments().getString("usuarioLogeado");
		mLibreta = getArguments().getParcelableArrayList("mLibreta");
		
		TextView title = (TextView)this.getActivity().findViewById(R.id.tv_title);
		TextView subtitle = (TextView)this.getActivity().findViewById(R.id.tv_subTitle);
		LinearLayout shadow = (LinearLayout)this.getActivity().findViewById(R.id.layout_shadow);
		shadow.setVisibility(LinearLayout.GONE);
		
		if (TextUtils.isEmpty(mTitleFrag)) {
			title.setText(mSubTitleFrag);
		} else {
			subtitle.setVisibility(View.VISIBLE);
			title.setText(mTitleFrag);
			title.setMaxLines(1);
			title.setTextSize(16);
			subtitle.setText(mSubTitleFrag);
		}
		
		LibretaFragmentPagerAdapter adapter = new LibretaFragmentPagerAdapter(getChildFragmentManager(), getActivity().getApplicationContext(), usuarioLogeado, mLibreta, mTitleFrag, mSubTitleFrag);
		
        ViewPager pager = (ViewPager)this.getActivity().findViewById(R.id.pager_libreta);
        pager.setAdapter(adapter);
        
        TabPageIndicator indicator = (TabPageIndicator)this.getActivity().findViewById(R.id.indicator_Libreta);        
        indicator.setViewPager(pager);
        
        if (adapter.getCount() > 1) {
        	if (mLibreta.get(0).getBimestre() == "0")
        		pager.setCurrentItem(adapter.getCount() - 1);
        	else
        		pager.setCurrentItem(Integer.parseInt(mLibreta.get(0).getBimestre()) - 1);
        }

	}
	
}
